﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace BanSupport
{

    public class UnityManager : MonoBehaviour
    {

        private static UnityManager _Instance;

        public static UnityManager Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new GameObject("UnityManager").AddComponent<UnityManager>();
                }
                return _Instance;
            }
        }

        public float globalYPos = 0;
        private Dictionary<BaseObject, UnityBaseObject> dicUnityBaseObjects = new Dictionary<BaseObject, UnityBaseObject>();

        private void Awake()
        {
            _Instance = this;
        }

        private void Start()
        {
            GameLogic.Instance.Init(str =>
            {
                Debug.Log(str);
            }, true);
        }

        private void Update()
        {

            GameLogic.Instance.Update();

            foreach (var aObject in GameLogic.Instance.gameData.allObjects)
            {
                if (!dicUnityBaseObjects.ContainsKey(aObject))
                {
                    dicUnityBaseObjects.Add(aObject, new UnityBaseObject(aObject));
                }
                else
                {
                    var unityBaseObject = dicUnityBaseObjects[aObject];
                    unityBaseObject.Update();
                    if (unityBaseObject.obj.destroy)
                    {
                        dicUnityBaseObjects.Remove(aObject);
                    }
                }
            }


            foreach (var aObject in GameLogic.Instance.gameData.allObjects)
            {
                if (Input.GetKeyDown(KeyCode.Space))
                {
                    aObject.RotateContinue((Fix64)90, (Fix64)5);
                }

                FixVector2 input = FixVector2.Zero;

                if (Input.GetKey(KeyCode.W))
                {
                    input.y += (Fix64)1;
                }
                if (Input.GetKey(KeyCode.S))
                {
                    input.y -= (Fix64)1;
                }
                if (Input.GetKey(KeyCode.A))
                {
                    input.x -= (Fix64)1;
                }
                if (Input.GetKey(KeyCode.D))
                {
                    input.x += (Fix64)1;
                }

                if (input == FixVector2.Zero)
                {
                    aObject.CancelMove();
                }
                else
                {
                    aObject.MoveTo(aObject.position + input * (Fix64)2, (Fix64)5);
                }


            }

        }

    }

}

